// 用户数据模块
import api from '@/common/request/index'
import store from '@/common/store'
import router from '@/common/router.js'
import tools from '@/common/utils/tools'

import {
	USER_INFO,
	TOKEN_INFO,
	OUT_LOGIN,
} from '../types.js'
const state = {
	userInfo: uni.getStorageSync('userInfo') ? uni.getStorageSync('userInfo') : {},
	tokenInfo: uni.getStorageSync('tokenInfo') ? uni.getStorageSync('tokenInfo') : '',
	bid:40,
	siteInfo: uni.getStorageSync('siteInfo') ? uni.getStorageSync('siteInfo') : {
					sitedata:{
						yc_num:'-',
						fj_num:'-',
						dj_num:'-',
						gx_num:'-'
					},
					category:[],
					update_time:0
	},
	chatScenesInfo: {},
	loginPopupShow: false,
	extConfig:uni.getStorageSync('ext_data') ? uni.getStorageSync('ext_data') : {
			bid:40,
			indexpagename:'index',//登陆后返回的页面
	}
}

const actions = {
	//设置token 获取个人信息 并返回上次页面
	setTokenAndBack({
		commit
	}, token) {
		
		uni.setStorageSync('tokenInfo', token);
		store.dispatch('getUserInfo');
        //返回登录之前的页面
		let fromLogin = uni.getStorageSync('fromLogin');
		if (fromLogin) {
			tools.routerTo(fromLogin.path, fromLogin.query, true);
			uni.removeStorageSync('fromLogin')
		} else {
			//默认不跳
			//router.replaceAll('/pages/index/my')
		}
	},
	
	// 获取用户信息
	getUserInfo({
		commit,state
	}) {
		
		return new Promise((resolve, reject) => {
			api('user.userinfo').then(res => {
				if (res.code==200){
					store.commit('USER_INFO',res.data);
					
            // #ifdef H5
            window.sessionStorage.setItem('userInfo', JSON.stringify(res.data));
			uni.setStorageSync('userInfo', res.data);
            // #endif
            // #ifndef H5
            uni.setStorageSync('userInfo', res.data);
            // #endif
					resolve(true)
				}else{
					uni.setStorageSync('userInfo', res.data);
					store.commit('OUT_LOGIN');
					resolve(false)
				}
			}).catch(e => {
				resolve(false)
				reject(e)
			})
			
		})
	},
	
	getsiteInfo({commit}, data) {
		console.log(Date.now())
		///let updatetime=store.state.user.siteInfo.update_time*1000
		return new Promise((resolve, reject) => {
			api('siteinit').then(res => {
				console.log('getsiteInfo===',res)
				if (res.code==200){
						setTimeout(() => {
							store.commit("setsiteInfo",res.data);
							resolve(res.data);
						}, 0);
				}else{
					resolve(false)
				}
			}).catch(e => {
				resolve(false)
				reject(e)
			})
		})
	},

}

const mutations = {
	[USER_INFO](state, data) {
		state.userInfo = data
	},
    [TOKEN_INFO](state, data) {
    	state.tokenInfo = data
    },
	[OUT_LOGIN](state, data) {
		uni.removeStorageSync('tokenInfo');
		uni.removeStorageSync('userInfo');
		store.commit('USER_INFO', {});
		store.commit('TOKEN_INFO', '');
	},

	  //微信场景参数
	  setChatScenesInfo(state, data) {
		  console.log('setChatScenesInfo',data)
		if (data) {
			state.chatScenesInfo = Object.assign({}, state.chatScenesInfo, data);
			if (data.query!=undefined){
				let querys=JSON.parse(decodeURIComponent(state.chatScenesInfo.query));
					state.chatScenesInfo.query=querys.q
				if (querys.b>0){
					state.bid=querys.b;
					uni.setStorageSync('bid', querys.b);
					store.commit('setbasebusinessID', state);
					store.dispatch('getUserInfo');
				}
				if (querys.s!=''&&querys.s!=undefined){
					state.chatScenesInfo.recommendCode=querys.s
					uni.setStorageSync('salt', querys.s);
				}
			}else{
					state.chatScenesInfo.recommendCode=uni.getStorageSync('salt');
			}
		}
	  },



	  setsiteInfo(state, data) {
		if (data) {
			uni.setStorageSync('siteInfo', data);
			state.siteInfo = data;
		}else{
				let nulldata = {
					sitedata:{
						yc_num:'-',
						fj_num:'-',
						dj_num:'-',
						gx_num:'-'
					},
					category:[],
					update_time:0
				};
			uni.setStorageSync('siteInfo', nulldata);
			state.siteInfo = nulldata;
		}
	  },
	  //登录弹窗状态
	  setroomPopupShow(state, data) {
		state.roomPopupShow = data;
	  },
	  setChatmsgnoread(state, data) {
		if (data) {
			state.chatmsgnoread = data;
		}
	  },
	  //登录弹窗状态
	  setLoginPopupShow(state, data) {
		  //console.log('close',data)
		state.loginPopupShow = data;
	  },
	  
	  
}

const getters = {
    loginFlag: state => {
        return Boolean( Object.keys(state.tokenInfo).length != 0 );
    },
}

export default {
	state,
	mutations,
	actions,
	getters
}
